package com.outsourcing.bottle.ui.fragment;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.adapter.ExchangeTimeLineAdapter;
import com.outsourcing.bottle.db.LanguageConfigTable;
import com.outsourcing.bottle.db.LoginUserInfoTable;
import com.outsourcing.bottle.db.PushNoticeInfoTable;
import com.outsourcing.bottle.domain.ExchangeTimelineInfo;
import com.outsourcing.bottle.domain.ExsessionsEntry;
import com.outsourcing.bottle.domain.LanguageConfig;
import com.outsourcing.bottle.domain.LoginUserInfoEntry;
import com.outsourcing.bottle.domain.PushNoticeInfo;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.ui.ChooseFriendActivity;
import com.outsourcing.bottle.ui.ExchangeInfoDetailActivity;
import com.outsourcing.bottle.ui.ExchangePicDetailActivity;
import com.outsourcing.bottle.ui.ExpandEditActivity;
import com.outsourcing.bottle.ui.HomeActivity;
import com.outsourcing.bottle.ui.SettingActivity;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.ServiceUtils;
import com.outsourcing.bottle.util.TextUtil;
import com.outsourcing.bottle.util.UICore;
import com.outsourcing.bottle.widget.CustomListView;
import com.outsourcing.bottle.widget.CustomPopupWindow;

/**
 * 
 * @author 06peng
 * 
 */
public class ExchangeFragment extends Fragment implements BasicUIEvent,
		OnClickListener, Callback, OnScrollListener {
	private static final String TAG = ExchangeFragment.class.getSimpleName();
	public static final int EXEU_GET_EXCHANGE_TIMELINE = 0;
	private static final int EXEU_REJECT_EX_SUCCESS = 3;
	private static final int EXEU_REJECT_EX_FAILED = 4;
	private static final int EXEU_ACCEPT_EX_SUCCESS = 5;
	private static final int EXEU_ACCEPT_EX_FAILED = 6;
	private static final int EXEU_VISIBLE_INFOEX_SUCCESS = 7;
	private static final int EXEU_VISIBLE_INFOEX_FAILED = 8;
	private static final int EXEU_INVISIBLE_INFOEX_SUCCESS = 9;
	private static final int EXEU_INVISIBLE_INFOEX_FAILED = 10;
	private static final int EXEU_REJECT_PICEX_SUCCESS = 11;
	private static final int EXEU_REJECT_PICEX_FAILED = 12;
	private static final int EXEU_VISIBLE_PICEX_SUCCESS = 13;
	private static final int EXEU_VISIBLE_PICEX_FAILED = 14;
	private static final int EXEU_INVISIBLE_PICEX_SUCCESS = 15;
	private static final int EXEU_INVISIBLE_PICEX_FAILED = 16;
	private static final int EXEU_LIKE_OP_PICEX_SUCCESS = 17;
	private static final int EXEU_LIKE_OP_PICEX_FAILED = 18;
	private static final int EXEU_GET_EXCHANGE_TIMELINE_MORE = 19;
	private static final int EXEU_GET_EXCHANGE_TIMELINE_MORE_NOTHING = 20;
	private static final int EXEU_GOTO_EXCHANGE = 21;
	private static final int INIT_HAS_EXS_ERROR = 22;
	private static final int EXEU_GOTO_PROFILE = 23;
	private static final int EXEU_GOTO_PHOTO = 24;
	private static final int INIT_HAS_EXS_PROFILE = 25;
	private static final int INIT_HAS_EXS_PHOTO = 26;
	private static final int EXEU_GET_SINGLE_EXSESSION = 27;
	private static final int EXEU_UNLIKE_OPERATION = 28;
	private static final int EXEU_LIKE_OPERATION = 29;
	private static final int EXEU_GET_DATA_FAILED = 30;
	private static final int EXEU_REFRESH_EXCHANGE_TIMELINE = 31;
	private static final int EXEU_GET_SINGLE_EXCHANGE_TIMELINE = 32;
	private static final int EXEU_REGET_TIMELINE = 33;
	private static final int EXEU_SET_BTPIC_PAINT = 34;
	private static final int EXEU_PUSH_NOTICE = 35;

	private int mode = 0;
	private int page = 1;

	private View mHeaderView = null;
	private FrameLayout mContentView = null;
	private Handler handler = null;
	private HomeActivity activity = null;
	private ExchangeTimeLineAdapter mExchangeAdapter;
	private CustomListView listView = null;
	private View loadMoreView = null;
	private List<ExsessionsEntry> exchangTimelineList = null;
	private ExchangeTimelineInfo exchangeTimelineInfo = null;
	private int visibleLastIndex = 0; // 最后的可视项索引
	private int visibleItemCount; // 当前窗口可见项总数

	public boolean init = false;

	private int ouid;
	private int exsType;
	public int exsid;
	private int picid;
	public int position;

	private View mLikeView;
	private CustomPopupWindow mLikePopview;
	ExsessionsEntry mExsessionsEntry;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		activity = (HomeActivity) getActivity();
		handler = new Handler(this);
		ServiceUtils.dout(TAG + "onCreate");

//		if (AppContext.getInstance().getCurrentItem() == 0) {
//			if (!init) {
//				ServiceUtils.dout(TAG + "   get first data....");
//				UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE, getString(R.string.init_data), null);
//			}
//		}
		if (!init) {
			ServiceUtils.dout(TAG + "   get first data....");
//			new Thread() {
//				public void run() {
//					getExchangeTimeLine(mode);
//				};
//			}.start();
			UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE, getString(R.string.init_data), null);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.root_layout, null);
		mContentView = (FrameLayout) view.findViewById(R.id.mainView);
//		mContentView.setonRefreshListener(new OnRefreshListener() {
//			
//			@Override
//			public void onRefresh() {
//			}
//		});
//		mContentView.setonScrollChangeListener(new OnScrollChangeBottom() {
//			
//			@Override
//			public void onScrollBottom() {
//			}
//		});

		if (!init) {
			listView = (CustomListView) LayoutInflater.from(activity).inflate(R.layout.main_refresh_list, null);
//			listView = (ListView)contentView.findViewById(R.id.list_view);
//			listView = (RefreshListView) view.findViewById(R.id.list_view);
			listView.setonRefreshListener(new CustomListView.OnRefreshListener() {
				
				@Override
				public void onRefresh() {
					page = 1;
					UICore.eventTask(ExchangeFragment.this, activity, EXEU_REFRESH_EXCHANGE_TIMELINE, null, null);
				}
			});
			loadMoreView = inflater.inflate(R.layout.refreshable_list_footer,
					null);
			loadMoreView.setVisibility(View.GONE);
			mHeaderView = LayoutInflater.from(activity).inflate(
					R.layout.exchange_list_header, null);
			listView.addHeaderView(mHeaderView);
			listView.addFooterView(loadMoreView);
			listView.setOnScrollListener(this);
		}
		initLikePopupWindow();
		return view;
//		
		
//		View view = inflater.inflate(R.layout.bottle_fragment, null);
//
//		listView = (RefreshListView) view.findViewById(R.id.list_view);
//		listView.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
//
//			@Override
//			public void onRefresh() {
////				refresh();
//			}
//		});
//		loadMoreView = inflater.inflate(R.layout.refreshable_list_footer, null);
//		loadMoreView.setVisibility(View.GONE);
//		mHeaderView = LayoutInflater.from(activity).inflate(
//				R.layout.exchange_list_header, null);
//		listView.addHeaderView(mHeaderView);
//		listView.addFooterView(loadMoreView);
//		listView.setOnScrollListener(this); // 添加滑动监听
//
//		initLikePopupWindow();
//		return view;
		
	}

	public void doGetExTimeline_Refresh() {
		UICore.eventTask(this, activity, EXEU_REFRESH_EXCHANGE_TIMELINE, "EXEU_GET_EXCHANGE_TIMELINE", null);
	}

	@Override
	public void onResume() {
		super.onResume();
		ServiceUtils.dout(TAG + "onResume");
//		if (init) {
//			Message message = Message.obtain(handler);
//			message.what = EXEU_REGET_TIMELINE;
//			handler.sendMessage(message);
//		}
		if (AppContext.getInstance().isFromExchangeTime() == true) {
			AppContext.getInstance().setFromExchangeTime(false);
			doGetSingleExsession(position, exsid);
		}
		if (AppContext.getInstance().isFromExchangeTimeToChangeFriend()) {
			AppContext.getInstance().setFromExchangeTimeToChangeFriend(
					false);
			page = 1;
			UICore.eventTask(ExchangeFragment.this, activity,
					EXEU_REFRESH_EXCHANGE_TIMELINE, "EXEU_GET_EXCHANGE_TIMELINE", null);
		}

	}

	@Override
	public void onPause() {
		super.onStop();
//		if (null != mContentView) {
//			mContentView.removeAllViews();
//		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case EXEU_GET_EXCHANGE_TIMELINE:
			setListHeader();
			if (null != mContentView) {
				mContentView.removeAllViews();
			}
			mContentView.addView(listView);
//			if (init) {
//				listView.setSelection(visibleLastIndex - visibleItemCount);
//			}
			init = true;
			new Thread() {
				public void run() {
					getPushNotice();
				};
			}.start();
			break;
		case  EXEU_REGET_TIMELINE:
			if (null != mContentView) {
				mContentView.removeAllViews();
			}
			mContentView.addView(listView);
			listView.setSelection(visibleLastIndex - visibleItemCount+1);
			break;
		case EXEU_REFRESH_EXCHANGE_TIMELINE:
			((HomeActivity) getActivity()).pushNoticeUpdateUI();
//			mExchangeAdapter.notifyDataSetChanged();
			setListHeader();
			if (null != mContentView) {
				mContentView.removeAllViews();
			}
			mContentView.addView(listView);
			listView.onRefreshComplete();
			new Thread() {
				public void run() {
					getPushNotice();
				};
			}.start();
//			listView.setSelection(1);
			break;
		case EXEU_PUSH_NOTICE:
			((HomeActivity) getActivity()).pushNoticeUpdateUI();
			break;
		case EXEU_GET_EXCHANGE_TIMELINE_MORE:
			
			mExchangeAdapter.notifyDataSetChanged();
			// listView.setSelection(visibleLastIndex - visibleItemCount + 1);
			// // 设置选中项
			break;
		case EXEU_GET_SINGLE_EXCHANGE_TIMELINE:
			
			mExchangeAdapter.notifyDataSetChanged();
			break;
		case EXEU_REJECT_EX_SUCCESS:
			 doGetSingleExsession(position, exsid);
//			page = 1;
//			UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE,
//					getString(R.string.init_data), null);
			activity.onToast((String) msg.obj);
			break;
			
		case EXEU_REJECT_EX_FAILED:
			activity.onToast((String) msg.obj);
			break;
		case EXEU_ACCEPT_EX_SUCCESS:
			 doGetSingleExsession(position, exsid);
//			page = 1;
//			UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE,
//					getString(R.string.init_data), null);
			activity.onToast((String) msg.obj);
			break;
		case EXEU_SET_BTPIC_PAINT:
			doGetSingleExsession(position, exsid);
			activity.onToast((String) msg.obj);
			break;
		case EXEU_ACCEPT_EX_FAILED:
			activity.onToast((String) msg.obj);
			break;
		case EXEU_VISIBLE_INFOEX_SUCCESS:
			 doGetSingleExsession(position, exsid);
//			page = 1;
//			UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE,
//					getString(R.string.init_data), null);
			activity.onToast((String) msg.obj);
			break;
		case EXEU_VISIBLE_INFOEX_FAILED:
			activity.onToast((String) msg.obj);
			break;
		case EXEU_INVISIBLE_INFOEX_FAILED:
			activity.onToast((String) msg.obj);
			break;
		case EXEU_INVISIBLE_INFOEX_SUCCESS:
			 doGetSingleExsession(position, exsid);
//			page = 1;
//			UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE,
//					getString(R.string.init_data), null);
			activity.onToast((String) msg.obj);
			break;
		case EXEU_REJECT_PICEX_SUCCESS:
			 doGetSingleExsession(position, exsid);
//			page = 1;
//			UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE,
//					getString(R.string.init_data), null);
			activity.onToast((String) msg.obj);
			break;
		case EXEU_REJECT_PICEX_FAILED:
			activity.onToast((String) msg.obj);
			break;
		case EXEU_VISIBLE_PICEX_SUCCESS:
			 doGetSingleExsession(position, exsid);
//			page = 1;
//			UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE,
//					getString(R.string.init_data), null);
			activity.onToast((String) msg.obj);
			break;
		case EXEU_VISIBLE_PICEX_FAILED:
			activity.onToast((String) msg.obj);
			break;
		case EXEU_INVISIBLE_PICEX_SUCCESS:
			 doGetSingleExsession(position, exsid);
//			page = 1;
//			UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE,
//					getString(R.string.init_data), null);
			activity.onToast((String) msg.obj);
			break;
		case EXEU_INVISIBLE_PICEX_FAILED:
			activity.onToast((String) msg.obj);
			break;
		case EXEU_LIKE_OP_PICEX_SUCCESS:
			doGetSingleExsession(position, exsid);
			activity.onToast((String) msg.obj);
			break;
		case EXEU_LIKE_OP_PICEX_FAILED:
			activity.onToast((String) msg.obj);
			break;
		case INIT_HAS_EXS_ERROR:
			activity.onToast((String) msg.obj);
			break;
		case EXEU_GET_EXCHANGE_TIMELINE_MORE_NOTHING:
			activity.onToast(TextUtil.R("no_more_data"));
			loadMoreView.setVisibility(View.GONE);
			// listView.setSelection(visibleLastIndex - visibleItemCount);
			break;
		case EXEU_GOTO_PROFILE:
			Intent intentExchangePro = new Intent(activity,
					ExchangeInfoDetailActivity.class);
			intentExchangePro.putExtra("ouid", ouid);
			intentExchangePro.putExtra("page", 1);
			activity.startActivity(intentExchangePro);
			break;
		case EXEU_GOTO_PHOTO:
			Intent intentExchangePic = new Intent(activity,
					ExchangePicDetailActivity.class);
			intentExchangePic.putExtra("ouid", ouid);
			intentExchangePic.putExtra("page", 1);
			activity.startActivity(intentExchangePic);
			break;
		case INIT_HAS_EXS_PROFILE:
			Intent intentProfile = new Intent(activity,
					ExpandEditActivity.class);
			Bundle bundleProfile = new Bundle();
			bundleProfile.putString("from_type", AppContext.REPLY_INFOEX);
			bundleProfile.putString("reply_infoex_type", "apply");
			bundleProfile.putInt("ouid", ouid);
			intentProfile.putExtras(bundleProfile);
			startActivity(intentProfile);
			break;
		case INIT_HAS_EXS_PHOTO:
			Intent intentPhoto = new Intent(activity, ExpandEditActivity.class);
			Bundle bundlePhoto = new Bundle();
			bundlePhoto.putString("from_type", AppContext.APPLY_PICEX);
			bundlePhoto.putInt("ouid", ouid);
			intentPhoto.putExtras(bundlePhoto);
			startActivity(intentPhoto);
			break;
		case EXEU_GET_DATA_FAILED:
			activity.onToast((String) msg.obj);
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_profile_info:
			activity.onToast("profile");
			Intent settingIntent = new Intent(activity, SettingActivity.class);
			startActivity(settingIntent);
			break;
		case R.id.iv_exphoto:
			activity.onToast("exphoto");
			break;
		case R.id.iv_exprofile:
			activity.onToast("exprofile");
			break;
		case R.id.iv_unlike:// 不喜欢
			mLikePopview.dismiss();
			UICore.eventTask(this, activity, EXEU_UNLIKE_OPERATION, "", null);
			break;
		case R.id.iv_like:// 喜欢
			mLikePopview.dismiss();
			UICore.eventTask(this, activity, EXEU_LIKE_OPERATION, "", null);
			break;
		default:
			break;
		}

	}

	private void setListHeader() {

			LanguageConfigTable languageConfigTable = new LanguageConfigTable();
			LanguageConfig languageCon = languageConfigTable.getBottleLanguageConfig();
			
			RelativeLayout mProfileInfo = (RelativeLayout) mHeaderView
					.findViewById(R.id.rl_tlime_profile_bottle);
			View line_profile = (View)mHeaderView.findViewById(R.id.line_tlime_profile_bottle);
			RelativeLayout mExProfile = (RelativeLayout) mHeaderView
					.findViewById(R.id.rl_tlime_exchange_profile);
			View line_exchange_profile = (View)mHeaderView.findViewById(R.id.line_exchange_profile);
			mExProfile.setVisibility(View.GONE);
			RelativeLayout mExPhoto = (RelativeLayout) mHeaderView
					.findViewById(R.id.rl_exchange_photo);
			mExPhoto.setVisibility(View.GONE);
			View line_exchange_photo = (View)mHeaderView.findViewById(R.id.line_exchange_photo);
			LoginUserInfoTable loginUserInfoTable = new LoginUserInfoTable();
			final LoginUserInfoEntry loginUserInfoEntry = loginUserInfoTable
					.getLoginUserInfo(AppContext.getInstance().getLogin_uid());

			if (null != loginUserInfoEntry
					&& loginUserInfoEntry.getBasicinfo_fullfill() == 0) {
				mProfileInfo.setVisibility(View.VISIBLE);
				line_profile.setVisibility(View.VISIBLE);
				RemoteImageView img_head = (RemoteImageView) mHeaderView
						.findViewById(R.id.iv_author_photo_3);
				img_head.setOnClickListener(this);
				LoginUserInfoTable userInfoTable = new LoginUserInfoTable();
				if (null != userInfoTable.getLoginUserInfo(AppContext
						.getInstance().getLogin_uid())) {
					String avatar = userInfoTable.getLoginUserInfo(
							AppContext.getInstance().getLogin_uid())
							.getAvatar();
					if (!TextUtils.isEmpty(avatar)) {
						img_head.setDefaultImage(R.drawable.avatar_default_small);
						img_head.setImageUrl(avatar);
					}
				}
				ImageView img_Profile = (ImageView) mHeaderView
						.findViewById(R.id.iv_profile_info);
				img_Profile.setOnClickListener(this);
				TextView tvProfileTips = (TextView) mHeaderView
						.findViewById(R.id.tv_bottle_profile_tips_2);
				tvProfileTips.setText(loginUserInfoEntry.getBasicinfo_tip());
				Button bt_profile = (Button) mHeaderView
						.findViewById(R.id.bt_profile_info_try);
				bt_profile.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent settingIntent = new Intent(activity,
								SettingActivity.class);
						startActivity(settingIntent);
						
					}
				});
			} else {
				mProfileInfo.setVisibility(View.GONE);
				line_profile.setVisibility(View.GONE);
			}

			if (exchangeTimelineInfo.getShowtips().getShow_infoex_tips() == 1) {
				mExProfile.setVisibility(View.VISIBLE);
				line_exchange_profile.setVisibility(View.VISIBLE);
				ImageView iv_exprofile = (ImageView) mHeaderView
						.findViewById(R.id.iv_exprofile);
				iv_exprofile.setOnClickListener(this);
				RemoteImageView img_head = (RemoteImageView) mHeaderView
						.findViewById(R.id.iv_author_photo_1);
				img_head.setOnClickListener(this);
				if (null != loginUserInfoTable.getLoginUserInfo(AppContext
						.getInstance().getLogin_uid())) {
					String avatar = loginUserInfoTable.getLoginUserInfo(
							AppContext.getInstance().getLogin_uid())
							.getAvatar();
					if (!TextUtils.isEmpty(avatar)) {
						img_head.setDefaultImage(R.drawable.avatar_default_small);
						img_head.setImageUrl(avatar);
					}
				}
				
				String infoTips1 = null;
				String infoTips2 = null;
				if (AppContext.language == 1) {
					infoTips1 = languageCon.getExchange_profile_tip_cn();
					infoTips2 = languageCon.getExchange_profile_tip_2_cn();
				}else {
					infoTips1 = languageCon.getExchange_profile_tip_en();
					infoTips2 = languageCon.getExchange_profile_tip_2_en();
				}
				TextView tvInfoTips = (TextView) mHeaderView
						.findViewById(R.id.tv_exchange_info_tips);
				tvInfoTips.setText(infoTips1);
				TextView tvInfoTip2 = (TextView) mHeaderView
						.findViewById(R.id.tv_exchange_info_tips_2);
				tvInfoTip2.setText(infoTips2);
				
				Button btExProfile = (Button) mHeaderView
						.findViewById(R.id.bt_exchange_profile_try);
				btExProfile.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						if (null != loginUserInfoEntry
								&& loginUserInfoEntry.getBasicinfo_fullfill() == 0) {
							String basicinfo_tip = loginUserInfoEntry.getBasicinfo_tip();
							AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
							alertDialog.setMessage(basicinfo_tip);
							alertDialog.setPositiveButton(TextUtil.R("confirm_ok"),
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(DialogInterface dialog, int which) {
											Intent intent = new Intent(activity, SettingActivity.class);
											startActivity(intent);
										}
									});
							alertDialog.setNegativeButton(TextUtil.R("confirm_cancel"), new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									
								}
							});
							alertDialog.create().show();

						} else {
							Intent intent = new Intent(activity,
									ChooseFriendActivity.class);
							intent.putExtra("choose_friend_type",
									AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE);
							startActivity(intent);	
						}
					}
				});
			} else {
				mExProfile.setVisibility(View.GONE);
				line_exchange_profile.setVisibility(View.GONE);
			}
			if (exchangeTimelineInfo.getShowtips().getShow_picex_tips() == 1) {
				mExPhoto.setVisibility(View.VISIBLE);
				line_exchange_photo.setVisibility(View.VISIBLE);
				RemoteImageView img_head = (RemoteImageView) mHeaderView
						.findViewById(R.id.iv_author_photo_2);
				img_head.setOnClickListener(this);
				if (null != loginUserInfoTable.getLoginUserInfo(AppContext
						.getInstance().getLogin_uid())) {
					String avatar = loginUserInfoTable.getLoginUserInfo(
							AppContext.getInstance().getLogin_uid())
							.getAvatar();
					if (!TextUtils.isEmpty(avatar)) {
						img_head.setDefaultImage(R.drawable.avatar_default_small);
						img_head.setImageUrl(avatar);
					}
				}

				String photoTips1 = null;
				String photoTips2 = null;
				if (AppContext.language == 1) {
					photoTips1 = languageCon.getExchange_photo_tip_cn();
					photoTips2 = languageCon.getExchange_photo_tip_2_cn();
				}else {
					photoTips1 = languageCon.getExchange_photo_tip_en();
					photoTips2 = languageCon.getExchange_photo_tip_2_en();
				}
				TextView tvPhotoTips = (TextView) mHeaderView
						.findViewById(R.id.tv_exchange_photo_tips);
				tvPhotoTips.setText(photoTips1);
				TextView tvPhotoTip2 = (TextView) mHeaderView
						.findViewById(R.id.tv_exchange_photo_tips_2);
				tvPhotoTip2.setText(photoTips2);
				
				ImageView iv_exphoto = (ImageView) mHeaderView
						.findViewById(R.id.iv_exphoto);
				iv_exphoto.setOnClickListener(this);
				Button btExPhoto = (Button) mHeaderView
						.findViewById(R.id.bt_exchange_photo_try);
				btExPhoto.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(activity,
								ChooseFriendActivity.class);
						intent.putExtra("choose_friend_type",
								AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE);
						startActivity(intent);
					}
				});

			} else {
				mExPhoto.setVisibility(View.GONE);
				line_exchange_photo.setVisibility(View.GONE);
			}
		if (null != exchangTimelineList) {
			mExchangeAdapter = new ExchangeTimeLineAdapter(activity, this,
					exchangTimelineList);
			listView.setAdapter(mExchangeAdapter);
//			Utility.setListViewHeightBasedOnChildren(listView);
		}
		
		if (mProfileInfo.getVisibility() == View.VISIBLE && mExProfile.getVisibility() == View.VISIBLE && mExPhoto.getVisibility() == View.VISIBLE ) {
			line_exchange_photo.setVisibility(View.GONE);
		}else if (mProfileInfo.getVisibility() == View.VISIBLE && mExProfile.getVisibility() == View.VISIBLE && mExPhoto.getVisibility() == View.GONE) {
			line_exchange_profile.setVisibility(View.GONE);
		} else if (mProfileInfo.getVisibility() == View.VISIBLE && mExProfile.getVisibility() == View.GONE && mExPhoto.getVisibility() == View.GONE) {
			line_profile.setVisibility(View.GONE);
		} else if (mProfileInfo.getVisibility() == View.GONE && mExProfile.getVisibility() == View.VISIBLE && mExPhoto.getVisibility() == View.VISIBLE) {
			line_exchange_photo.setVisibility(View.GONE);
		} else if (mProfileInfo.getVisibility() == View.GONE && mExProfile.getVisibility() == View.VISIBLE && mExPhoto.getVisibility() == View.GONE) {
			line_exchange_profile.setVisibility(View.GONE);
		} else if (mProfileInfo.getVisibility() == View.GONE && mExProfile.getVisibility() == View.GONE && mExPhoto.getVisibility() == View.VISIBLE) {
			line_exchange_photo.setVisibility(View.GONE);
		}
		
		
	}

	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case EXEU_GET_EXCHANGE_TIMELINE:
			getExchangeTimeLine(mode);
			break;
		case EXEU_REFRESH_EXCHANGE_TIMELINE:
			refreshExchangeTimeLine(mode);
			break;
		case EXEU_GET_EXCHANGE_TIMELINE_MORE:
			getExchangeTimeLineMore(mode, page);
			break;
		case EXEU_GOTO_EXCHANGE:
			getHasExs(exsType, ouid);
			break;
		case EXEU_REJECT_EX_SUCCESS:
			rejectInfoex(ouid);
			break;
		case EXEU_ACCEPT_EX_SUCCESS:
			acceptInfoex(ouid);
			break;
		case EXEU_VISIBLE_INFOEX_SUCCESS:
			visibleInfoex(ouid);
			break;
		case EXEU_INVISIBLE_INFOEX_SUCCESS:
			invisibleInfoex(ouid);
			break;
		case EXEU_REJECT_PICEX_SUCCESS:
			rejectPicex(ouid);
			break;
		case EXEU_VISIBLE_PICEX_SUCCESS:
			visiblePicex(ouid);
			break;
		case EXEU_INVISIBLE_PICEX_SUCCESS:
			invisiblePicex(ouid);
			break;
		case EXEU_GET_SINGLE_EXSESSION:
			getSingleExchange(exsid, position);
			break;
		case EXEU_LIKE_OPERATION:
			likeIOperationPicex(ouid, picid, 0);
			break;
		case EXEU_UNLIKE_OPERATION:
			likeIOperationPicex(ouid, picid, 1);
			break;
		case EXEU_SET_BTPIC_PAINT:
			int optype = (Integer)obj;
			setExpicPaint(ouid, picid, optype);
			break;
		default:
			break;
		}
	}

	private void getExchangeTimeLine(int mode) {
		if (AppContext.getInstance().getLogin_uid() == 0
				|| AppContext.getInstance().getLogin_token() == null) {
			initConfig();
		}
		StringBuffer url = new StringBuffer(UrlConfig.get_ex_timeline);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&mode=" + mode);
		url.append("&count=10");
		url.append("&page=1");
		url.append("&picfeedscount=5");
		url.append("&feedscount=5");
		ServiceUtils.dout("getExchangeTimeline url: " + url.toString());
		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getExchangeTimeline results: " + results);
			Gson gson = new Gson();
			exchangeTimelineInfo = gson.fromJson(results,
					ExchangeTimelineInfo.class);
			if (exchangeTimelineInfo.getResults().getAuthok() == 1) {
				exchangTimelineList = exchangeTimelineInfo.getExsessions_list();
					Message message = Message.obtain(handler);
					message.what = EXEU_GET_EXCHANGE_TIMELINE;
					handler.sendMessage(message);
			} else {
				Message message = handler.obtainMessage(EXEU_GET_DATA_FAILED,
						exchangeTimelineInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void refreshExchangeTimeLine(int mode) {
		StringBuffer url = new StringBuffer(UrlConfig.get_ex_timeline);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&mode=" + mode);
		url.append("&count=10");
		url.append("&page=1");
		url.append("&picfeedscount=5");
		url.append("&feedscount=5");
		ServiceUtils.dout("refreshExchangeTimeline url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getExchangeTimeline results: " + results);
			Gson gson = new Gson();
			exchangeTimelineInfo = gson.fromJson(results,
					ExchangeTimelineInfo.class);
			if (exchangeTimelineInfo.getResults().getAuthok() == 1) {
				exchangTimelineList = exchangeTimelineInfo.getExsessions_list();
				handler.sendEmptyMessage(EXEU_REFRESH_EXCHANGE_TIMELINE);
			} else {
				Message message = handler.obtainMessage(EXEU_GET_DATA_FAILED,
						exchangeTimelineInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void getPushNotice() {
		StringBuffer url = new StringBuffer(UrlConfig.push_notice);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				JSONObject obj = new JSONObject(result);
				JSONObject resultObj = obj.getJSONObject("results");
				if (resultObj.getInt("authok") == 1) {
					JSONObject pushObj = obj.getJSONObject("push_notice");
					PushNoticeInfoTable table = new PushNoticeInfoTable();
					PushNoticeInfo pushInfo = new PushNoticeInfo();
					table.clearTable();
					pushInfo.setMessage_avatar(pushObj.getString("message_avatar"));
					pushInfo.setMessage_content(pushObj.getString("message_content"));
					pushInfo.setNew_btfeed_count(pushObj.getInt("new_btfeed_count"));
					pushInfo.setNew_exfeed_count(pushObj.getInt("new_exfeed_count"));
					pushInfo.setNew_message_count(pushObj.getInt("new_message_count"));
					pushInfo.setNew_notice_count(pushObj.getInt("new_notice_count"));
					pushInfo.setNew_maybeknow_count(pushObj.getInt("new_maybeknow_count"));
					table.savePushNoticeInfo(pushInfo, AppContext.getInstance().getLogin_uid());
					
					Message message = Message.obtain(handler);
					message.what = EXEU_PUSH_NOTICE;
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getExchangeTimeLineMore(int mode, int page) {
		StringBuffer url = new StringBuffer(UrlConfig.get_ex_timeline);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&mode=" + mode);
		url.append("&count=10");
		url.append("&page=" + page);
		url.append("&picfeedscount=5");
		url.append("&feedscount=5");
		ServiceUtils.dout("getExchangeTimelineMore url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getExchangeTimelineMore results: " + results);
			Gson gson = new Gson();
			exchangeTimelineInfo = gson.fromJson(results,
					ExchangeTimelineInfo.class);
			if (exchangeTimelineInfo.getResults().getAuthok() == 1) {
				// exchangTimelineList =
				// exchangeTimelineInfo.getExsessions_list();
				if (null != exchangeTimelineInfo.getExsessions_list()
						&& exchangeTimelineInfo.getExsessions_list().size() > 0) {
					exchangTimelineList.addAll(exchangeTimelineInfo
							.getExsessions_list());
					Message message = Message.obtain(handler);
					message.what = EXEU_GET_EXCHANGE_TIMELINE_MORE;
					handler.sendMessage(message);
				} else {
					Message message = Message.obtain(handler);
					message.what = EXEU_GET_EXCHANGE_TIMELINE_MORE_NOTHING;
					handler.sendMessage(message);
				}
			} else {
				Message message = handler.obtainMessage(EXEU_GET_DATA_FAILED,
						exchangeTimelineInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			e.printStackTrace();
			Message message = Message.obtain(handler);
			message.what = EXEU_GET_EXCHANGE_TIMELINE_MORE_NOTHING;
			handler.sendMessage(message);
		}
	}

	public void doGetSingleExsession(int position, int exsid) {
		this.exsid = exsid;
		this.position = position;
		UICore.eventTask(ExchangeFragment.this, activity, EXEU_GET_SINGLE_EXSESSION,
				getString(R.string.init_data), null);
	}

	private void getSingleExchange(int exsid, int position) {
		StringBuffer url = new StringBuffer(UrlConfig.get_ex_timeline);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&mode=4&exsid=" + exsid);
		url.append("&picfeedscount=5");
		url.append("&feedscount=5");
		ServiceUtils.dout("getSingleExchange url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			if (results == null) {
				return;
			}
			ServiceUtils.dout("getSingleExchange results: " + results);
			Gson gson = new Gson();
			ExchangeTimelineInfo exchangeTimelineInfo = gson.fromJson(results,
					ExchangeTimelineInfo.class);
			if (exchangeTimelineInfo.getResults().getAuthok() == 1) {
				List<ExsessionsEntry> mSingleExList = exchangeTimelineInfo
						.getExsessions_list();

				if (null != mSingleExList && mSingleExList.size() > 0) {
					mExsessionsEntry = mSingleExList.get(0);
					exchangTimelineList.set(position, mExsessionsEntry);
					Message message = Message.obtain(handler);
					message.what = EXEU_GET_SINGLE_EXCHANGE_TIMELINE;
					handler.sendMessage(message);
				}
			} else {
				Message message = handler.obtainMessage(EXEU_GET_DATA_FAILED,
						exchangeTimelineInfo.getResults().getErrmsg());
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doRejectInfoex(int ouid, int position, int exsid) {
		this.ouid = ouid;
		this.position = position;
		this.exsid = exsid;
		UICore.eventTask(this, activity, EXEU_REJECT_EX_SUCCESS, "", null);
	}

	private void rejectInfoex(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.reject_infoex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);

		ServiceUtils.dout("rejectInfoex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("rejectInfoex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_REJECT_EX_SUCCESS, successmsg);
					handler.sendMessage(message);

				} else {
					Message message = handler.obtainMessage(
							EXEU_REJECT_EX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doAcceptInfoex(int ouid, int position, int exsid) {
		this.ouid = ouid;
		this.position = position;
		this.exsid = exsid;

		LoginUserInfoTable loginUserInfoTable = new LoginUserInfoTable();
		LoginUserInfoEntry loginUserInfoEntry = loginUserInfoTable
				.getLoginUserInfo(AppContext.getInstance().getLogin_uid());

		if (null != loginUserInfoEntry
				&& loginUserInfoEntry.getBasicinfo_fullfill() == 0) {
			String basicinfo_tip = loginUserInfoEntry.getBasicinfo_tip();
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
			alertDialog.setMessage(basicinfo_tip);
			alertDialog.setPositiveButton(TextUtil.R("confirm_ok"),
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(activity, SettingActivity.class);
							startActivity(intent);
						}
					});
			alertDialog.setNegativeButton(TextUtil.R("confirm_cancel"), new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
			alertDialog.create().show();

		} else {
			UICore.eventTask(this, activity, EXEU_ACCEPT_EX_SUCCESS, "", null);
		}

	}

	private void acceptInfoex(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.accept_infoex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);

		ServiceUtils.dout("acceptInfoex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("acceptInfoex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_ACCEPT_EX_SUCCESS, successmsg);
					handler.sendMessage(message);

				} else {
					Message message = handler.obtainMessage(
							EXEU_ACCEPT_EX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doVisibleInfoex(int ouid, int position, int exsid) {
		this.ouid = ouid;
		this.position = position;
		this.exsid = exsid;
		UICore.eventTask(this, activity, EXEU_VISIBLE_INFOEX_SUCCESS, "", null);
	}

	private void visibleInfoex(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.visible_infoex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);

		ServiceUtils.dout("visibleInfoex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("visibleInfoex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_VISIBLE_INFOEX_SUCCESS, successmsg);
					handler.sendMessage(message);

				} else {
					Message message = handler.obtainMessage(
							EXEU_VISIBLE_INFOEX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doInvisibleInfoex(int ouid, int position, int exsid) {
		this.ouid = ouid;
		this.position = position;
		this.exsid = exsid;
		UICore.eventTask(this, activity, EXEU_INVISIBLE_INFOEX_SUCCESS, "",
				null);
	}

	private void invisibleInfoex(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.invisible_infoex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);

		ServiceUtils.dout("invisibleInfoex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("invisibleInfoex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_INVISIBLE_INFOEX_SUCCESS, successmsg);
					handler.sendMessage(message);

				} else {
					Message message = handler.obtainMessage(
							EXEU_INVISIBLE_INFOEX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doRejectPicex(int ouid, int position, int exsid) {
		this.ouid = ouid;
		this.position = position;
		this.exsid = exsid;
		UICore.eventTask(this, activity, EXEU_REJECT_PICEX_SUCCESS, "", null);
	}

	private void rejectPicex(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.reject_picex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);

		ServiceUtils.dout("rejectPicex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("rejectPicex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_REJECT_PICEX_SUCCESS, successmsg);
					handler.sendMessage(message);

				} else {
					Message message = handler.obtainMessage(
							EXEU_REJECT_PICEX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doVisiblePicex(int ouid, int position, int exsid) {
		this.ouid = ouid;
		this.position = position;
		this.exsid = exsid;
		UICore.eventTask(this, activity, EXEU_VISIBLE_PICEX_SUCCESS, "", null);
	}

	private void visiblePicex(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.visible_picex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);

		ServiceUtils.dout("visiblePicex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("visiblePicex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_VISIBLE_PICEX_SUCCESS, successmsg);
					handler.sendMessage(message);

				} else {
					Message message = handler.obtainMessage(
							EXEU_VISIBLE_PICEX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doInvisiblePicex(int ouid, int position, int exsid) {
		this.ouid = ouid;
		this.position = position;
		this.exsid = exsid;
		UICore.eventTask(this, activity, EXEU_INVISIBLE_PICEX_SUCCESS, "", null);
	}

	private void invisiblePicex(int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.invisible_picex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);

		ServiceUtils.dout("invisiblePicex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("invisiblePicex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_INVISIBLE_PICEX_SUCCESS, successmsg);
					handler.sendMessage(message);

				} else {
					Message message = handler.obtainMessage(
							EXEU_INVISIBLE_PICEX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doExchange(int exsType, int ouid) {
		this.ouid = ouid;
		this.exsType = exsType;
		UICore.eventTask(this, activity, EXEU_GOTO_EXCHANGE, "has_exs", null);
	}

	private void getHasExs(int exsType, int ouid) {
		StringBuffer url = new StringBuffer(UrlConfig.get_has_exs);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);

		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				JSONObject object = new JSONObject(result);
				JSONObject resultObj = object.getJSONObject("results");
				int authok = resultObj.getInt("authok");
				if (authok == 0) {
					String errmsg = resultObj.getString("errmsg");
					Message msg = Message.obtain(handler, INIT_HAS_EXS_ERROR, errmsg);
					handler.sendMessage(msg);
				} else {
					JSONObject hasExsObj = object.getJSONObject("has_exs");
					if (exsType == AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE) {
						int has_infoexs = hasExsObj.getInt("has_infoexs");
						if (has_infoexs == 1) {
							handler.sendEmptyMessage(EXEU_GOTO_PROFILE);
						} else {
							handler.sendEmptyMessage(INIT_HAS_EXS_PROFILE);
						}
					} else if (exsType == AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE) {
						int has_picexs = hasExsObj.getInt("has_picexs");
						if (has_picexs == 1) {
							handler.sendEmptyMessage(EXEU_GOTO_PHOTO);
						} else {
							handler.sendEmptyMessage(INIT_HAS_EXS_PHOTO);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void likeIOperationPicex(int ouid, int picid, int liketype) {
		StringBuffer url = new StringBuffer(UrlConfig.likeop_picex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		url.append("&picid=" + picid);
		url.append("&liketype=" + liketype);

		ServiceUtils.dout("likeIOperationPicex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("likeIOperationPicex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_LIKE_OP_PICEX_SUCCESS, successmsg);
					handler.sendMessage(message);
				} else {
					Message message = handler.obtainMessage(
							EXEU_LIKE_OP_PICEX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 喜欢pop */
	private void initLikePopupWindow() {

		LayoutInflater mLayoutInflater = LayoutInflater.from(activity);
		mLikeView = mLayoutInflater.inflate(R.layout.pop_bottle_like, null);
		ImageView iv_unlike = (ImageView) mLikeView
				.findViewById(R.id.iv_unlike);
		ImageView iv_like = (ImageView) mLikeView.findViewById(R.id.iv_like);
		iv_unlike.setOnClickListener(this);
		iv_like.setOnClickListener(this);
		mLikePopview = new CustomPopupWindow(mLikeView,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mLikePopview.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.like_frame_right));
		mLikePopview.setOutsideTouchable(true);
		mLikePopview.update();
		mLikePopview.setTouchable(true);
		mLikePopview.setFocusable(true);
	}

	public void showInfoPopupWindow(View achorView, int picid, int ouid,
			int exsid,int position) {
		this.ouid = ouid;
		this.picid = picid;
		this.exsid = exsid;
		this.position = position;
		if (mLikePopview.isShowing()) {
			mLikePopview.dismiss();
		} else {
			// mLikePopview.showAtLocation(achorView, Gravity.RIGHT, 0, 0);
			mLikePopview.showAsDropDown(achorView, -150, -70);
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

	}

	public void doMenuItemSelect(int mode) {
		this.mode = mode;
		this.page = 1;
		UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE, "", null);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		listView.setFirstVisiableItem(firstVisibleItem);
		this.visibleItemCount = visibleItemCount;
		visibleLastIndex = firstVisibleItem + visibleItemCount;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		try {
			if (mExchangeAdapter != null) {
				int itemsLastIndex = mExchangeAdapter.getCount() + 2; // 数据集最后一项的索引
				int lastIndex = itemsLastIndex+1; // 加上底部的loadMoreView项 和 head项
				ServiceUtils.dout(TAG + " visibleLastIndex:" + visibleLastIndex);
				ServiceUtils.dout(TAG + "        lastIndex:" + lastIndex);
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
					// 如果是自动加载,可以在这里放置异步加载数据的代码
					page++;
					loadMoreView.setVisibility(View.VISIBLE);
					UICore.eventTask(this, activity, EXEU_GET_EXCHANGE_TIMELINE_MORE, null, null);
					Log.i("LOADMORE", "loading...");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void initConfig() {
    	String country = getResources().getConfiguration().locale.getCountry();
		if (country.equals("CN")) {
			AppContext.setLanguage(1);
		} else {
			AppContext.setLanguage(0);
		}
    	
    	SharedPreferences preferences = getActivity().getSharedPreferences("Preferences_userinfo", Activity.MODE_APPEND);
    	int login_uid = preferences.getInt("login_uid", 0);
    	String login_token = preferences.getString("login_token", null);
    	AppContext.getInstance().setLogin_uid(login_uid);
		AppContext.getInstance().setLogin_token(login_token);
    }
	
	public void doExpicPaint(int ouid, int picid,int optype,int exsid,int position){
		this.ouid = ouid;
		this.picid = picid;
		this.exsid = exsid;
		this.position = position;
		UICore.eventTask(this, activity, EXEU_SET_BTPIC_PAINT, "", optype);
	}
	/**
	 * 
	 * @param ouid
	 * @param picid
	 * @param optype  设置是否允许涂鸦。选择项：不允许（0）、允许（1）
	 */
	private void setExpicPaint(int ouid, int picid,int optype) {
		StringBuffer url = new StringBuffer(UrlConfig.set_expic_paint);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		url.append("&picid=" + picid);
		url.append("&optype=" + optype);
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				JSONObject object = new JSONObject(result);
				JSONObject resultObj = object.getJSONObject("results");
				int authok = resultObj.getInt("success");
				if (authok == 0) {
					String errmsg = resultObj.getString("errmsg");
					Message msg = Message.obtain(handler, EXEU_GET_DATA_FAILED, errmsg);
					handler.sendMessage(msg);
				} else {
					String successmsg = resultObj.getString("successmsg");
					Message msg = Message.obtain(handler, EXEU_SET_BTPIC_PAINT, successmsg);
					handler.sendMessage(msg);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
}
